package ljl.alg.hot100;

import commons.ListNode;

public class _206_reverse_list {
    
    
    public ListNode reverseList2(ListNode head) {
        ListNode prev = null, cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
    
    // 这个递归的我背的很熟了
    // 但是有点不太理解为什么可以工作？
    public ListNode reverseList(ListNode head) {
        // 这里忽略了 head 至少两个元素
        if (head == null || head.next == null) return head;
        ListNode res = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return res;
    }
}
